package application;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SelectionMode;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class LIstViewDemo extends Application{
	
	private String[] flagTitles = {"Canada", "China", "UK"};


	private ImageView[] flagImages = {
			new ImageView("Canada.jpg"), 
			new ImageView("Flag.png"), 
			new ImageView("UK.jpeg")
			};
	
	
	@Override
	public void start(Stage primaryStage) {
		
		for (ImageView flag : flagImages) {
			flag.setFitHeight(27.30 * 5);
			flag.setFitWidth(40.96 * 5);
		}
		
		
		ListView<String> lv = new ListView<>(FXCollections.observableArrayList(flagTitles));
		lv.setPrefSize(200, 400);
		lv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
		
		FlowPane imagePane = new FlowPane(10, 10);
		BorderPane pane = new BorderPane();
		pane.setLeft(new ScrollPane(lv));
		//pane.setLeft(lv);
		pane.setCenter(imagePane);
		
		lv.getSelectionModel().selectedItemProperty().addListener(ov -> {
			imagePane.getChildren().clear();
			for (Integer i: lv.getSelectionModel().getSelectedIndices()) {
				imagePane.getChildren().add(flagImages[i]);
			}
		});
		
		Scene scene = new Scene(pane, 450, 170);
		primaryStage.setTitle("ListViewDemo");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	
	public static void main(String[] args) {
		Application.launch(args);
	}
}
